<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends Controller {

    public function index() {
        if (session('userid') > 0) {
            $url = C('SITE_URL');
            if (cookie("area") > 0) {
                $url = U("Area/lists", array("id" => cookie("area")));
            }
            echo "<script>document.location.href='" . $url . "';</script>";
        }
        $this->assign("ctitle", '新用户注册');
        $this->assign("config", getTableConfig());
        $threeLogin = getTableFile("accounts");
        $this->assign("threeLogin", $threeLogin);
        $this->display("Index:reg");
    }

    public function reg_post() {
        header("Content-Type:text/html; charset=utf-8");
        $data['addtime'] = time();
        $data['name'] = trim(I('post.name'));
        $data['pwd'] = md5(I('post.password'));
        $data['email'] = I('post.email');

        $data['loginip'] = get_client_ip();
        $data['logintime'] = time();
        $info = M('user')->where("name='" . $data['name'] . "' or  email='" . $data['email'] . "'")->find();
        if($data['name'] == ''){
            $this->error('用户名不能为空！');
        }
        if ($info['id'] > 0) {
            $this->error('该账号已被注册！');
        }
        $lastid = M('user')->add($data);
         if ($lastid) {
            session('username', $data['name']);
            session('userid', $lastid);
            cookie('userid', $lastid);
            $url = C('SITE_URL');
            if (cookie("area") > 0) {
                $url = U("Area/lists", array("id" => cookie("area")));
            }
            if (session('type') != '') {
                $three = M('user')->field(session('type'), 'id')->where("" . session('type') . "= " . session("openid") . "")->find();
                if (empty($three)) {
                    M('user')->where("id = " . session("userid") . "")->save(array(session('type')=>session("openid")));
//                    echo M('user')->getlastsql();exit;
                    $config = getTableConfig();
                    $points_three = $config["points_" . session('type')];
                    M('user')->where("id=" . session('userid') . "")->setInc('money', $points_three);
                    addLog("bind_three", $points_three, session('userid'), "绑定" . session('type') . "获得" . $points_three . "积分", 5, 1);
                } else {
                    if ($info2['id'] != $info['id']) {
                        $this->error("已经被其他账户绑定！");
                        exit;
                    }
                }
                session('type', null);
                session('openid', null);
            }
             $this->success('注册成功！', $url);
        } else {
            $this->error('注册失败，请联系管理员！');
        }
    }

}

?>
